package cn.interestingshop.dao;

import cn.interestingshop.entity.Classify;
import cn.interestingshop.entity.User;
import cn.interestingshop.utils.EmptyUtils;
import cn.interestingshop.utils.Md5Util;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @ClassName : UserDaoImpl
 * @Description :
 * @Author : Lzz
 * @Date: 2023/2/20 14:11:39
 */
public class UserDaoImpl extends BaseDaoImpl implements UserDao {

    public UserDaoImpl(Connection connection) {
        super(connection);
    }

    /**
     * 查询用户列表
     * @param currentPageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<User> findAllUserList(Integer currentPageNo, Integer pageSize) {
        List<User> userList = new ArrayList<>();    //返回的用户集合
        ResultSet rs = null;
        try {
            String sql = "select id,account,password,nick_name,gender,id_card_no,email,phone,type from t_user  ";
            sql += "limit "+(currentPageNo-1)*pageSize+","+pageSize;
            rs = this.executeQuery(sql, null);
            while (rs.next()){
                User user = createEntityByResultSet(rs);
                userList.add(user);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
                this.closeResource(rs);
                this.closeResource();
        }
        return userList;
    }

    /**
     * 查询用户总记录条数
     * @return
     */
    @Override
    public int findUserCount() {
        ResultSet rs = null;
        int count = 0;      //总记录条数
        try {
            String sql = "select count(1) from t_user ";
            rs = this.executeQuery(sql,null);
            if(rs.next()){
                count = rs.getInt(1);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            this.closeResource(rs);
            this.closeResource();
        }
        return count;
    }

    @Override
    public User selectOne(Integer id) {

        ResultSet resultSet=null;
        User user=new User();

        try{
            String sql="select * from t_user where id="+id;

            resultSet=executeQuery(sql,null);

            if(resultSet.next()){
                user=createEntityByResultSet(resultSet);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(resultSet);
        }

        return user;


    }

    @Override
    public int selectOneAccount(String account) {
        ResultSet resultSet=null;
        int rs=0;

        try{
            String sql="select count(1) from t_user where account=?";
            Object[] objects=new Object[]{account};
            resultSet=executeQuery(sql,objects);

            if(resultSet.next()){
                rs=resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(resultSet);
        }

        return rs;
    }

    @Override
    public int selectOnePhone(String phone) {
        ResultSet resultSet=null;
        int rs=0;

        try{
            String sql="select count(1) from t_user where phone=?";
            Object[] objects=new Object[]{phone};
            resultSet=executeQuery(sql,objects);

            if(resultSet.next()){
                rs=resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(resultSet);
        }

        return rs;
    }

    @Override
    public int update(User user) {

        int rs=0;

        try{
            String sql;
            Object[] objects;
            if(!EmptyUtils.isEmpty(user.getId())){
                sql="update t_user set account=?,nick_name=?,id_card_no=?,email=?,phone=?,type=? where id=?";
                objects=new Object[]{user.getAccount(),user.getNickName(),user.getIdCardNo(),user.getEmail(),user.getPhone(),user.getType(),user.getId()};
            }else{
                sql="INSERT INTO `interesting_shop_db`.`t_user`(`account`, `nick_name`, `password`,`id_card_no`, `email`, `phone`, `type`) VALUES (?,?,?,?,?,?,?)";
                objects=new Object[]{user.getAccount(),user.getNickName(),user.getPassword(),user.getIdCardNo(),user.getEmail(),user.getPhone(),user.getType()};
            }


            rs=executeUpdate(sql,objects);
        }catch (Exception e){
            e.printStackTrace();;
        }finally {
            closeResource();
        }

        return rs;
    }

    @Override
    public int delete(Integer id) {
        int rs=0;

        try{
            String sql;
            sql="delete from t_user where id="+id;
            rs=executeUpdate(sql,null);
        }catch (Exception e){
            e.printStackTrace();;
        }finally {
            closeResource();
        }

        return rs;
    }

    @Override
    public int selectOne(String account, String password) {
        ResultSet resultSet=null;
        int rs=0;

        try{

        String sql="select id from t_user where account=? and password=?";
        Object[] objects=new Object[]{account,password};
        resultSet=executeQuery(sql,objects);
        if(resultSet.next()){
            rs=resultSet.getInt(1);
        }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(resultSet);
        }
        return rs;
    }

    @Override
    public int insert(User user) {
        int rs=0;

        String password= Md5Util.md5(user.getPassword());

        try {
            String sql = "INSERT INTO `interesting_shop_db`.`t_user`(`account`, `nick_name`, `password`, `gender`, `id_card_no`, `email`, `phone`, `type`) VALUES (?,?,?,?,?,?,?,?)";
            Object[] objects = new Object[]{user.getAccount(), user.getNickName(), password, user.getGender(), user.getIdCardNo(), user.getEmail(), user.getPhone(), user.getType()};
            rs = executeUpdate(sql, objects);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
        }

        return rs;
    }

    @Override
    public int selectOne(String phone) {
        ResultSet resultSet=null;
        int rs=0;

        try{

            String sql="select id from t_user where phone=?";
            Object[] objects=new Object[]{phone};
            resultSet=executeQuery(sql,objects);
            if(resultSet.next()){
                rs=resultSet.getInt(1);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(resultSet);
        }
        return rs;
    }


    @Override
    public User createEntityByResultSet(ResultSet rs) throws Exception {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setAccount(rs.getString("account"));
        user.setPassword(rs.getString("password"));
        user.setNickName(rs.getString("nick_name"));
        user.setGender(rs.getInt("gender"));
        user.setIdCardNo(rs.getString("id_card_no"));
        user.setEmail(rs.getString("email"));
        user.setPhone(rs.getString("phone"));
        user.setType(rs.getInt("type"));
        return user;
    }
}
